import pandas as pd
import numpy as np
from django.db.models import Q
from accounts.models import StudentSurvey


class DataProcessor:
    def __init__(self):
        self.model = StudentSurvey

    def clean_data(self, data):
        """清理数据"""
        # 移除空值
        cleaned_data = data.dropna()
        return cleaned_data

    def normalize_data(self, data):
        """标准化数据"""
        # 对数值型数据进行标准化
        numeric_data = data.select_dtypes(include=[np.number])
        normalized_data = (numeric_data - numeric_data.mean()) / numeric_data.std()
        return normalized_data

    def process_survey_data(self):
        """处理问卷数据"""
        # 获取所有问卷数据
        queryset = self.model.objects.all().values()
        df = pd.DataFrame(list(queryset))

        # 数据清理
        df = self.clean_data(df)

        # 处理分类数据
        # TODO: 根据实际需求处理分类数据

        return df

    def get_filtered_data(self, filters=None):
        """根据条件获取过滤后的数据"""
        query = Q()
        if filters:
            for field, value in filters.items():
                query &= Q(**{field: value})

        queryset = self.model.objects.filter(query).values()
        return pd.DataFrame(list(queryset))

    def prepare_analysis_data(self, features=None):
        """准备用于分析的数据"""
        df = self.process_survey_data()

        if features:
            df = df[features]

        return self.normalize_data(df)
